<?php
/**
 * 用户表Model类
 * @author:fanjunjun
 * @date：2017 11 17
 */

namespace app\index\model;
use think\Model;
class User extends Base {
    //获取用户类型
    public function getRoleId()
    {
        return  array(
            '1' => "admin",
            '2' => "主播",
            '3' => "用户",
        );
    }
    /**
     * 添加用户
     * 正确返回用户id
     * 未知错误返回0
     */
    public function addUser($data)
    {
        $data['salt'] = rand(111111, 999999);
        $data['password'] = $this->encryptPassword($data['password'], $data['salt']);
        $data['ctime'] = time();

        $where['username'] = $data['username'];
        if ($this->where($where)->count() > 0) {
            return -1;
        } else {
           $status = 0;
            return $this->addData($data,$status);
        }
    }

    // 处理密码
    public function encryptPassword($password = "111111", $salt = '111111')
    {
        return md5(md5($password) . $salt);
    }

    /**
     * 登录鉴权
     * @return user_id正确,-1用户不存在,-2密码错误
     */
    public function checkLogin($username, $password) {
        $data = $this->where(array('username' => $username))->select();
        $data = $data[0];
        if ($data == false) {
            return -1;
        }
        $pwd = $this->encryptPassword($password, $data['salt']);
        if ($pwd != $data['password']) {
            return -2;
        }
        return $data['id'];
    }

    /**
     * 获取用户的信息
     */
    public function getSelfInfo($user_id) {
        $data = $this->where(array('id' => $user_id))->select();
        $data = $data[0];
        return $this->parse($data, true);
    }
    private function parse($data, $selfFlag = false, $adminFlag = false)
    {
        $ret = array();
        $ret['id'] = intval($data['id']);
        $ret['name'] = strval($data['name']);
        $ret['username'] = strval($data['username']);
        $ret['type'] = intval($data['type']);
        $type_name = $this->getRoleId();
        $ret['role_name'] = strval($type_name[$ret['type']]);
        $ret['telephone'] = strval($data['telephone']);
        if ($selfFlag || $adminFlag) {
            $ret['name'] = strval($data['username']);
            $ret['telephone'] = strval($data['telephone']);
            $ret['status'] = intval($data['status']);
        }
        return $ret;
    }

    public function tokenEncode($id, $name, $expire)
    {
        $data = array();
        $data['id'] = $id;
        $data['name'] = $name;
        $data['expire'] = $expire;
        $data['now'] = time();
        return \houdunwang\crypt\Crypt::encrypt(json_encode($data), config('DATA_CRYPT_KEY'));
    }

    public function tokenDecode($token)
    {
        $data = \houdunwang\crypt\Crypt::decrypt($token, C('DATA_CRYPT_KEY'));
        return json_decode($data, true);
    }

    public function checkPassword($id, $pwd)
    {
        $map = array('id' => $id);
        $user = $this->where($map)->find();
        if (!$user) {
            return false;
        }
        $pwd = $this->encryptPassword($pwd, $user['salt']);
        $password = $user['password'];
        if ($password != $pwd) {
            return false;
        }
        return true;
    }

    public function forceResetPwd($id, $pwd, &$status)
    {
        $status = 0;
        $map = array('id' => $id);
        $user = $this->where($map)->find();
        if (!$user) {
            $status = 1;
            return false;
        }
        $salt = $user['salt'];
        $data['password'] = $this->encryptPassword($pwd, $salt);
        $res = $this->where('id=' . $id)->save($data);
        if ($res === false) {
            $status = 3;
            return false;
        }
        return true;
    }

    public function updateData1($where,$data) {

        return $this->where($where)->save($data);
    }
}